package org.aersoft.jokebook.core;

import java.util.LinkedList;
import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;

/**
 * Class to get the Google Account informations.
 * @author Danilo de Godoy Carolino.
 */
public class GoogleAccount {

	/**
	 * Account manager instance of {@link AccountManager} type.
	 */
	private AccountManager accountManager;

	/**
	 * A account list of {@link Account} type.
	 */
	private Account[] accounts;

	/**
	 * The user email of {@link String} type.
	 */
	private String email;

	/**
	 * The user name of {@link String} type.
	 */
	private String username;

	/**
	 * Constructor method of class.
	 * @param activity of {@link Activity} type.
	 */
	public GoogleAccount(Activity activity) {
		this.accountManager = AccountManager.get(activity);
		this.accounts = accountManager
				.getAccountsByType(Constants.TYPE_GOOGLE_ACCOUNT);
		this.setEmail();
		this.setUsername();
	}

	/**
	 * Get the email of Google Account.
	 * @return the email of {@link String} type.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Set the email of Google Acount.
	 */
	private void setEmail() {
		List<String> accounts = new LinkedList<String>();
		for (Account account : this.accounts)
			accounts.add(account.name);
		this.email = accounts.get(0);
	}

	/**
	 * Get the username of Google Account.
	 * @return the username of {@link String} type.
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Set the username of Google Account.
	 */
	public void setUsername() {
		String[] parts = email.split(Constants.AT);
		this.username = parts[0];
	}
}